<?php

namespace app\admin\model\hd\activity;

use think\Model;
use app\admin\model\department\Department;
use app\admin\library\Auth;


class Info extends Model
{





    // 表名
    protected $name = 'hd_activity_info';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;

    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'forms_text',
        'reg_start_time_text',
        'reg_end_time_text',
        'start_time_text',
        'end_time_text',
        'status_text',
        'create_time_text',
        'update_time_text',
        'type',
        'department'
    ];



    public function getFormsList()
    {
        return ['1' => __('志愿活动'), '2' => __('投票活动')];
    }

    public function getStatusList()
    {
        return ['normal' => __('Normal'), 'hidden' => __('Hidden')];
    }

    // public function getType()
    // {
    //     return $this->belongsTo('app\admin\model\hd\activity\Type', 'type_id', 'id')->setEagerlyType(0);
    // }
    // public function getDepartment()
    // {
    //     return $this->belongsTo('app\admin\model\department\Department', 'department_id', 'id');
    // }
    public function getFormsTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['forms']) ? $data['forms'] : '');
        $list = $this->getFormsList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getRegStartTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['reg_start_time']) ? $data['reg_start_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getRegEndTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['reg_end_time']) ? $data['reg_end_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getStartTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['start_time']) ? $data['start_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getEndTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['end_time']) ? $data['end_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getCreateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['create_time']) ? $data['create_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    public function getUpdateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['update_time']) ? $data['update_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setRegStartTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setRegEndTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setStartTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setEndTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setCreateTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setUpdateTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }
    protected function setAdminIdAttr($value)
    {
        $auth = Auth::instance();
        return $auth->isLogin() ? $auth->id : null;
    }

    public function type()
    {
        return $this->belongsTo('app\admin\model\hd\activity\Type', 'type_id', 'id')->setEagerlyType(0);
    }

    // public function department()
    // {
    //     $ids = explode(',', $this->department_id);
    //     return \app\admin\model\department\Department::whereIn('id', $ids)->select();
    // }
    public function getDepartmentAttr()
    {
        $ids = explode(',', $this->department_id);
        return Department::whereIn('id', $ids)->select();
    }
    public function log()
    {
        return $this->belongsTo('app\admin\model\hd\activity\Log', 'id', 'act_id')->setEagerlyType(0);
    }
}
